package com.allprs.hmcs.service.impl;

import com.allprs.common.core.domain.AjaxResult;
import com.allprs.common.utils.StringUtils;
import com.allprs.hmcs.entity.*;
import com.allprs.hmcs.enums.NaturalTypeEnum;
import com.allprs.hmcs.mapper.*;
import com.allprs.hmcs.pojo.reqBo.CockpitQueryBo;
import com.allprs.hmcs.pojo.reqBo.DrainageQueryBo;
import com.allprs.hmcs.pojo.resBo.NameSelectRes;
import com.allprs.hmcs.service.IDrainageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wjf
 * @描述: 排水设施service
 * @date 2024年06月12日
 * @version: 1.0
 */

@Service
public class DrainageServiceImpl implements IDrainageService {

    @Resource
    private CulvertMapper culvertMapper;

    @Resource
    private DamMapper damMapper;

    @Resource
    private PipeMapper pipeMapper;

    @Resource
    private PumpStationMapper pumpStationMapper;

    @Resource
    private NaturalWaterMapper naturalWaterMapper;

    @Resource
    private CatchmentMapper catchmentMapper;

    @Override
    public AjaxResult getDrainageInfo(DrainageQueryBo queryBo) {

        String type = queryBo.getType();
        if(StringUtils.isBlank(type)){
            return AjaxResult.error("查询类型不能为空！");
        }

        if(type.equals(NaturalTypeEnum.CULVERT.getCode())){
            //涵闸
            QueryWrapper<Culvert> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                    .eq(queryBo.getId() != 0, "id", queryBo.getId());

            List<Culvert> results = culvertMapper.selectList(queryWrapper);
            return AjaxResult.success(results);
        }else if(type.equals(NaturalTypeEnum.PUMP_STATION.getCode())){
            //泵站
            QueryWrapper<PumpStation> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                    .eq(queryBo.getId() != 0, "id", queryBo.getId());

            List<PumpStation> results = pumpStationMapper.selectList(queryWrapper);
            return AjaxResult.success(results);

        }else if(type.equals(NaturalTypeEnum.PIPE.getCode())){
            //管网
            QueryWrapper<Pipe> queryWrapper = new QueryWrapper<>();
            //管网的返回第一条数据
//            queryWrapper.like(queryBo.getName() != null, "name", queryBo.getName())
//                    .eq(queryBo.getId() != 0, "id", queryBo.getId());

            List<Pipe> results = pipeMapper.selectList(queryWrapper);
            return AjaxResult.success(results);

        }else if(type.equals(NaturalTypeEnum.DAM.getCode())){
            //堤防
            QueryWrapper<Dam> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                    .eq(queryBo.getId() != 0, "id", queryBo.getId());

            List<Dam> results = damMapper.selectList(queryWrapper);
            return AjaxResult.success(results);

        }else{
            return AjaxResult.error("类型错误");
        }
    }

    @Override
    public AjaxResult getSelectByName(CockpitQueryBo queryBo) {

        String type = queryBo.getType();
        if(StringUtils.isBlank(type)){
            return AjaxResult.error("类型不能为空！");
        }

        if(type.equals(NaturalTypeEnum.RIVER.getCode()) || type.equals(NaturalTypeEnum.CANAL.getCode())){
            QueryWrapper<NaturalWater> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                    .eq(StringUtils.isNotBlank(queryBo.getType()), "type", queryBo.getType());

            List<NaturalWater> naturalWaters = naturalWaterMapper.selectList(queryWrapper);

            return AjaxResult.success(naturalWaters);

        }else if(type.equals(NaturalTypeEnum.CULVERT.getCode())){
            //涵闸
            QueryWrapper<Culvert> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName());

            List<Culvert> results = culvertMapper.selectList(queryWrapper);
            return AjaxResult.success(results);
        }else if(type.equals(NaturalTypeEnum.PUMP_STATION.getCode())){
            //泵站
            QueryWrapper<PumpStation> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName());

            List<PumpStation> results = pumpStationMapper.selectList(queryWrapper);
            return AjaxResult.success(results);

        }else if(type.equals(NaturalTypeEnum.PIPE.getCode())){
            //管网
            QueryWrapper<Pipe> queryWrapper = new QueryWrapper<>();
            //管网的返回第一条数据
//            queryWrapper.like(queryBo.getName() != null, "name", queryBo.getName())
//                    .eq(queryBo.getId() != 0, "id", queryBo.getId());

            List<Pipe> results = pipeMapper.selectList(queryWrapper);
            return AjaxResult.success(results);

        }else if(type.equals(NaturalTypeEnum.DAM.getCode())){
            //堤防
            QueryWrapper<Dam> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName());

            List<Dam> results = damMapper.selectList(queryWrapper);
            return AjaxResult.success(results);

        }else if(type.equals(NaturalTypeEnum.CATCHMENT.getCode())){
            QueryWrapper<Catchment> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName());
            List<Catchment> catchmentList = catchmentMapper.selectList(queryWrapper);
            return AjaxResult.success(catchmentList);

        }else{
            return AjaxResult.error("类型错误");
        }

//        List<NameSelectRes> result = new ArrayList<>();
//        //首先模糊匹配自然水体的数据
//        QueryWrapper<NaturalWater> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(queryBo.getName() != null, "name", queryBo.getName());
//        List<NaturalWater> naturalWaters = naturalWaterMapper.selectList(queryWrapper);
//        if(CollectionUtils.isNotEmpty(naturalWaters)){
//            for (NaturalWater naturalWater : naturalWaters) {
//                NameSelectRes vo = new NameSelectRes();
//                vo.setId(naturalWater.getId());
//                vo.setName(naturalWater.getName());
//                vo.setType(naturalWater.getType());
//                result.add(vo);
//            }
//        }
//
//        //涵闸数据匹配
//        QueryWrapper<Culvert> queryWrapperCulvert = new QueryWrapper<>();
//        queryWrapperCulvert.like(queryBo.getName() != null, "name", queryBo.getName());
//        List<Culvert> resultsCulvert = culvertMapper.selectList(queryWrapperCulvert);
//        if(CollectionUtils.isNotEmpty(resultsCulvert)){
//            for (Culvert culvert : resultsCulvert) {
//                NameSelectRes vo = new NameSelectRes();
//                vo.setId(culvert.getId());
//                vo.setName(culvert.getName());
//                vo.setType(NaturalTypeEnum.CULVERT.getCode());
//                result.add(vo);
//            }
//        }
//
//        //泵站数据匹配
//        QueryWrapper<PumpStation> queryWrapperPump = new QueryWrapper<>();
//        queryWrapperPump.like(queryBo.getName() != null, "name", queryBo.getName());
//        List<PumpStation> resultsPump = pumpStationMapper.selectList(queryWrapperPump);
//        if(CollectionUtils.isNotEmpty(resultsPump)){
//            for (PumpStation pumpStation : resultsPump) {
//                NameSelectRes vo = new NameSelectRes();
//                vo.setId(pumpStation.getId());
//                vo.setName(pumpStation.getName());
//                vo.setType(NaturalTypeEnum.PUMP_STATION.getCode());
//                result.add(vo);
//            }
//        }
//
//        //管网数据匹配
//        QueryWrapper<Pipe> queryWrapperPipe = new QueryWrapper<>();
//        queryWrapperPipe.like(queryBo.getName() != null, "name", queryBo.getName());
//        List<Pipe> resultsPipe = pipeMapper.selectList(queryWrapperPipe);
//        if(CollectionUtils.isNotEmpty(resultsPipe)){
//            for (Pipe pipe : resultsPipe) {
//                NameSelectRes vo = new NameSelectRes();
//                vo.setId(pipe.getId());
//                vo.setName(pipe.getName());
//                vo.setType(NaturalTypeEnum.PIPE.getCode());
//                result.add(vo);
//            }
//        }
//
//        //堤防数据匹配
//        QueryWrapper<Dam> queryWrapperDam = new QueryWrapper<>();
//        queryWrapperDam.like(queryBo.getName() != null, "name", queryBo.getName());
//        List<Dam> resultsDam = damMapper.selectList(queryWrapperDam);
//        if(CollectionUtils.isNotEmpty(resultsDam)){
//            for (Dam dam : resultsDam) {
//                NameSelectRes vo = new NameSelectRes();
//                vo.setId(dam.getId());
//                vo.setName(dam.getName());
//                vo.setType(NaturalTypeEnum.DAM.getCode());
//                result.add(vo);
//            }
//        }
//        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult getInfo(NameSelectRes queryBo) {
        String type = queryBo.getType();
        if(StringUtils.isBlank(type)){
            return AjaxResult.error("类型不能为空");
        }
        if(type.equals(NaturalTypeEnum.RIVER.getCode()) || type.equals(NaturalTypeEnum.CANAL.getCode())){
            //自然水体
            QueryWrapper<NaturalWater> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                    .eq(StringUtils.isNotBlank(queryBo.getType()), "type", queryBo.getType())
                    .eq(queryBo.getId() != 0, "id", queryBo.getId());
            List<NaturalWater> naturalWaters = naturalWaterMapper.selectList(queryWrapper);
            return AjaxResult.success(naturalWaters);
        }else if(type.equals(NaturalTypeEnum.CULVERT.getCode())){
            //涵闸
            QueryWrapper<Culvert> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                    .eq(queryBo.getId() != 0, "id", queryBo.getId());

            List<Culvert> results = culvertMapper.selectList(queryWrapper);
            return AjaxResult.success(results);
        }else if(type.equals(NaturalTypeEnum.PUMP_STATION.getCode())){
            //泵站
            QueryWrapper<PumpStation> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                    .eq(queryBo.getId() != 0, "id", queryBo.getId());

            List<PumpStation> results = pumpStationMapper.selectList(queryWrapper);
            return AjaxResult.success(results);

        }else if(type.equals(NaturalTypeEnum.PIPE.getCode())){
            //管网
            QueryWrapper<Pipe> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                    .eq(queryBo.getId() != 0, "id", queryBo.getId());

            List<Pipe> results = pipeMapper.selectList(queryWrapper);
            return AjaxResult.success(results);

        }else if(type.equals(NaturalTypeEnum.DAM.getCode())){
            //堤防
            QueryWrapper<Dam> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(queryBo.getName()), "name", queryBo.getName())
                    .eq(queryBo.getId() != 0, "id", queryBo.getId());

            List<Dam> results = damMapper.selectList(queryWrapper);
            return AjaxResult.success(results);

        }else{
            return AjaxResult.error("类型错误");
        }
    }
}
